package window.imp;

import DTO.ExcelDataDTO;
import com.melloware.jintellitype.JIntellitype;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.EtchedBorder;

import handle.HotKeyRegistrant;
import util.FrameUtil;
import window.ConfigFrame;
import window.ExcelPasterFrame;
import window.TopFrame;

/**
 * Swing based test application to test all the functions of the JIntellitype
 * library.
 * <p>
 * Copyright (c) 1999-2019 Melloware, Inc. <http://www.melloware.com>
 * @author Emil A. Lefkof III <mellowaredev@gmail.com>
 * @version 1.4.0
 *
 * 全局热键功能来源于JIntellitype
 * 主窗口直接在其实例类上进行修改
 */
public class DefaultExcelPasterFrame extends ExcelPasterFrame {

	private static final long serialVersionUID = 1L;
	private static final int SHIFT_A = 88;
	private static final int SHIFT_D = 89;
	private static final int SHIFT_W = 90;
	private static final int SHIFT_S = 91;

	private final JButton btnSelectFile = new JButton();
	private final JButton btnRegisterHotKey = new JButton();
	private final JButton btnUnregisterHotKey = new JButton();
	private final JButton btnShowInfo = new JButton();
	private final JPanel bottomPanel = new JPanel();
	private final JPanel mainPanel = new JPanel();
	private final JPanel topPanel = new JPanel();
	private final JScrollPane scrollPane = new JScrollPane();
	private final JTextArea textArea = new JTextArea();


	private Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

	/**
	 * Creates new form.
	 */
	public DefaultExcelPasterFrame(){

		initComponents();
		initJIntellitype();
	}

	/**
	 * 自定义参数,使用默认参数的部分用null补齐
	 * @param topFrame 自定义的topFrame
	 * @param configFrame 自定义的configFrame
	 */
	public DefaultExcelPasterFrame(TopFrame topFrame, ConfigFrame configFrame, HotKeyRegistrant hotKeyRegistrant){

		if (topFrame != null) {
			super.topFrame = topFrame;
		}
		if (configFrame != null) {
			super.configFrame = configFrame;
		}
		if (hotKeyRegistrant != null) {
			super.hotKeyRegistrant = hotKeyRegistrant;
		}

		initComponents();
		initJIntellitype();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.melloware.jintellitype.HotkeyListener#onHotKey(int)
	 */
	public void onHotKey(int aIdentifier) {



		ExcelDataDTO dto = null;

		if(aIdentifier == SHIFT_A){
			dto = tool.getLastInfo();
		}else if(aIdentifier == SHIFT_D){
			dto = tool.getNextInfo();
		}else if(aIdentifier == SHIFT_W){
			dto = tool.getLastStu();
		}else if(aIdentifier == SHIFT_S){
			dto = tool.getNextStu();
		}

		if (dto == null) {
			output("读取完毕");

			if(topFrame != null){
				//通知信息窗口
				topFrame.endInfo();
			}
		}else {
			//将内容复制到剪切板
			clipboard.setContents(new StringSelection(dto.getText()),null);

			output("剪切板已更新为"+dto.getText());

			if (topFrame != null) {
				//通知信息窗口
				topFrame.showInfo(dto);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.melloware.jintellitype.IntellitypeListener#onIntellitype(int)
	 */
	public void onIntellitype(int aCommand) {

		switch (aCommand) {
		case JIntellitype.APPCOMMAND_BROWSER_BACKWARD:
			output("BROWSER_BACKWARD message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_BROWSER_FAVOURITES:
			output("BROWSER_FAVOURITES message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_BROWSER_FORWARD:
			output("BROWSER_FORWARD message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_BROWSER_HOME:
			output("BROWSER_HOME message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_BROWSER_REFRESH:
			output("BROWSER_REFRESH message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_BROWSER_SEARCH:
			output("BROWSER_SEARCH message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_BROWSER_STOP:
			output("BROWSER_STOP message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_LAUNCH_APP1:
			output("LAUNCH_APP1 message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_LAUNCH_APP2:
			output("LAUNCH_APP2 message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_LAUNCH_MAIL:
			output("LAUNCH_MAIL message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_MEDIA_NEXTTRACK:
			output("MEDIA_NEXTTRACK message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_MEDIA_PLAY_PAUSE:
			output("MEDIA_PLAY_PAUSE message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_MEDIA_PREVIOUSTRACK:
			output("MEDIA_PREVIOUSTRACK message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_MEDIA_STOP:
			output("MEDIA_STOP message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_VOLUME_DOWN:
			output("VOLUME_DOWN message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_VOLUME_UP:
			output("VOLUME_UP message received " + Integer.toString(aCommand));
			break;
		case JIntellitype.APPCOMMAND_VOLUME_MUTE:
			output("VOLUME_MUTE message received " + Integer.toString(aCommand));
			break;
		default:
			output("Undefined INTELLITYPE message caught " + Integer.toString(aCommand));
			break;
		}
	}


	/**
	 * Method to register a hotkey using the RegisterHotKey Windows API call.
	 * <p>
	 * 
	 * @param aEvent the ActionEvent fired.
	 */
	private void btnRegisterHotKey_actionPerformed(ActionEvent aEvent) {
		// assign the WINDOWS+A key to the unique id 88 for identification
		//为各个按键设定id
//		JIntellitype.getInstance().registerHotKey(SHIFT_A, JIntellitype.MOD_SHIFT, 'A');
//		JIntellitype.getInstance().registerHotKey(SHIFT_D, JIntellitype.MOD_SHIFT, 'D');
//		JIntellitype.getInstance().registerHotKey(SHIFT_W, JIntellitype.MOD_SHIFT, 'W');
//		JIntellitype.getInstance().registerHotKey(SHIFT_S, JIntellitype.MOD_SHIFT, 'S');

		// use a 0 for the modifier if you just want a single keystroke to be a
		// hotkey
//		JIntellitype.getInstance().registerHotKey(PRINT_SCREEN, 0, 44);
//		JIntellitype.getInstance().registerHotKey(F11, "F11");
//		JIntellitype.getInstance().registerHotKey(F12, JIntellitype.MOD_ALT, 123);
//		JIntellitype.getInstance().registerHotKey(SEMICOLON, 0, 186);
//		JIntellitype.getInstance().registerHotKey(TICK, 0, 192);
		// clear the text area
		hotKeyRegistrant.register();
		textArea.setText("");
//		output("RegisterHotKey WINDOWS+A was assigned uniqueID 88");
//		output("RegisterHotKey ALT+SHIFT+B was assigned uniqueID 89");
//		output("RegisterHotKey CTRL+SHIFT+C was assigned uniqueID 90");
//		output("RegisterHotKey PRINT_SCREEN was assigned uniqueID 91");
//		output("RegisterHotKey F9 was assigned uniqueID 92");
//		output("RegisterHotKey F12 was assigned uniqueID 93");
//		output("RegisterHotKey SEMICOLON was assigned uniqueID 94");
//		output("Press WINDOWS+A or ALT+SHIFT+B or CTRL+SHIFT+C in another application and you will see the debug output in the textarea.");
		output("注册热键SHIFT+A完成,此热键可选择当前位置左方的数据");
		output("注册热键SHIFT+D完成,此热键可选择当前位置右方的数据");
		output("注册热键SHIFT+W完成,此热键可选择上一行中第一个数据");
		output("注册热键SHIFT+S完成,此热键可选择下一行中第一个数据");
	}

	/**
	 * Method to unregister a hotkey using the UnregisterHotKey Windows API call.
	 * <p>
	 * 
	 * @param aEvent the ActionEvent fired.
	 */
	private void btnUnregisterHotKey_actionPerformed(ActionEvent aEvent) {
//		JIntellitype.getInstance().unregisterHotKey(WINDOWS_A);
//		JIntellitype.getInstance().unregisterHotKey(ALT_SHIFT_B);
//		JIntellitype.getInstance().unregisterHotKey(SHIFT_A);
//		JIntellitype.getInstance().unregisterHotKey(SHIFT_D);
//		JIntellitype.getInstance().unregisterHotKey(SHIFT_W);
//		JIntellitype.getInstance().unregisterHotKey(SHIFT_S);
//		JIntellitype.getInstance().unregisterHotKey(PRINT_SCREEN);
//		JIntellitype.getInstance().unregisterHotKey(F11);
//		JIntellitype.getInstance().unregisterHotKey(F12);
//		JIntellitype.getInstance().unregisterHotKey(SEMICOLON);
//		output("UnregisterHotKey WINDOWS+A");
//		output("UnregisterHotKey ALT+SHIFT+B");
//		output("UnregisterHotKey CTRL+SHIFT+C");
//		output("UnregisterHotKey PRINT_SCREEN");
//		output("UnregisterHotKey F9");
//		output("UnregisterHotKey F12");
//		output("UnregisterHotKey SEMICOLON");
//		output("Press WINDOWS+A or ALT+SHIFT+B in another application and you will NOT see the debug output in the textarea.");
		hotKeyRegistrant.unRegister();
	output("热键注册已取消");

	}

	private void btnShowInfo_actionPerformed(ActionEvent e){
		if (topFrame == null) {
			topFrame = new DefaultTopFrame();
		}else {
			topFrame.setVisible(true);
		}
	}

	private void btnSelectFile_actionPerformed(ActionEvent e){
		if (configFrame == null) {
			configFrame = new DefaultConfigFrame(btnRegisterHotKey);
		}else {
			configFrame.setVisible(true);
			configFrame.requestFocus();
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 */
	private void initComponents() {
		mainPanel.setLayout(new BorderLayout());
		topPanel.setBorder(new EtchedBorder(1));
		bottomPanel.setLayout(new BorderLayout());
		bottomPanel.setBorder(new EtchedBorder(1));

		btnSelectFile.setText("选择文件");
		btnSelectFile.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				btnSelectFile_actionPerformed(e);
			}
		});

		btnRegisterHotKey.setText("注册热键");
		btnRegisterHotKey.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnRegisterHotKey_actionPerformed(e);
			}
		});
		btnUnregisterHotKey.setText("取消注册");
		btnUnregisterHotKey.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnUnregisterHotKey_actionPerformed(e);
			}
		});

		btnRegisterHotKey.setEnabled(false);

		btnShowInfo.setText("信息窗口");
		btnShowInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnShowInfo_actionPerformed(e);
			}
		});


		topPanel.add(btnSelectFile);
		topPanel.add(btnRegisterHotKey);
		topPanel.add(btnUnregisterHotKey);


		scrollPane.getViewport().add(textArea);
		bottomPanel.add(scrollPane, BorderLayout.CENTER);
		bottomPanel.add(btnShowInfo,BorderLayout.SOUTH);
		mainPanel.add(topPanel, BorderLayout.NORTH);
		mainPanel.add(bottomPanel, BorderLayout.CENTER);

		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent evt) {
				// don't forget to clean up any resources before close
				JIntellitype.getInstance().cleanUp();
				System.exit(0);
			}
		});

		this.getContentPane().add(mainPanel);
		this.pack();
		this.setSize(800, 600);

		FrameUtil.center(this);
	}

	/**
	 * Initialize the JInitellitype library making sure the DLL is located.
	 */
	private void initJIntellitype() {
		try {

			// initialize JIntellitype with the frame so all windows commands can
			// be attached to this window
			JIntellitype.getInstance().addHotKeyListener(this);
			JIntellitype.getInstance().addIntellitypeListener(this);
			output("初始化完毕");
			output("欢迎使用Excel全局复制器,本软件启用后你可以在任何地方对设定好的表格进行快捷复制");
			output("单击选择文件以开始");
			output("文件选择完毕后请单击注册热键以启用全局复制功能");
		} catch (RuntimeException ex) {
			output("Either you are not on Windows, or there is a problem with the JIntellitype library!");
		}
	}

	/**
	 * Send the output to the log and the text area.
	 * <p>
	 * 
	 * @param text the text to output
	 */
	private void output(String text) {
		textArea.append(text);
		textArea.append("\n");
	}

}